﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_FieldsGroup: ContainerBase
    {
        private int _formDefaultLabelWidth = 2;
        private int _groupDefaultLabelWidth = 3;
        private string _formDefaultControlWidth = "";
        private string _groupDefaultControlWidth = "";// "70%";
        private ColEnum _col = ColEnum.Col_2;
        private string _title = "";
        private string _titleColor = "#3c8dbc";
        private int _titleFontSize = 18;
        private bool _isSetLabelWidth = false;

        public PageForm_FieldsGroup(ViewContext viewContext) : base(viewContext)
        {
            this.Css("form-group webui-form_formfield webui_form_fieldsgroup");
        }

        public PageForm_FieldsGroup DefaultLabelWidth(int width)
        {
            _isSetLabelWidth = true;
            _groupDefaultLabelWidth = width;

            return this;
        }

        public PageForm_FieldsGroup DefaultControlWidth(string width)
        {
            
            _groupDefaultControlWidth = width;

            return this;
        }

        public PageForm_FieldsGroup Column(ColEnum col)
        {
            _col = col;

            switch (col)
            {
                case ColEnum.Col_1:
                    break;
                case ColEnum.Col_2:
                    break;
                case ColEnum.Col_3:
                    if(!_isSetLabelWidth) DefaultLabelWidth(3);
                    break;
                default:
                    break;
            }

            return this;
        }

        public PageForm_FieldsGroup Title(string title)
        {
            _title = title;

            return this;
        }

        public PageForm_FieldsGroup TitleColor(string color)
        {
            _titleColor = color;

            return this;
        }

        public PageForm_FieldsGroup TitleFontSize(int fontSize)
        {
            _titleFontSize = fontSize;

            return this;
        }

        public override void Dispose()
        {
            ViewContext.ViewData["DefaultLabelWidth"]= _formDefaultLabelWidth;

            ViewContext.ViewData["DefaultControlWidth"] = _formDefaultControlWidth;

            ViewContext.ViewData.Remove("ColQty");

            ViewContext.Writer.WriteLine("</div>");

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"</div> <!-- outer border -->");
            }
        }

        internal override ContainerBase RenderInternal()
        {
            this._formDefaultLabelWidth = (int)ViewContext.ViewData["DefaultLabelWidth"];

            this._formDefaultControlWidth = (string)ViewContext.ViewData["DefaultControlWidth"];

            ViewContext.ViewData["DefaultLabelWidth"] = _groupDefaultLabelWidth;

            ViewContext.ViewData["DefaultControlWidth"] = _groupDefaultControlWidth;

            ViewContext.ViewData["ColQty"] = _col;

            if (_col== ColEnum.Col_1)
            {
                this.Css("webui_form_fieldsgroup_1");
            }
            else if (_col== ColEnum.Col_2)
            {
                this.Css("webui_form_fieldsgroup_2");
            }
            else if (_col == ColEnum.Col_3)
            {
                this.Css("webui_form_fieldsgroup_3");
            }

            if (_withOuterBorder)
            {
                ViewContext.Writer.WriteLine($"<div class=\"{_outerBorderCss}\">");
            }

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            if (!string.IsNullOrWhiteSpace(_title))
            {
                ViewContext.Writer.WriteLine($"<div class=\"webui_form_fieldsgroup_head\" style=\"color:{_titleColor};font-size:{_titleFontSize}px;\">{_title}</div>");
            }

            return this;
        }

        public enum ColEnum
        {
            Col_1,

            Col_2,

            Col_3
        }
    }
}
